/**
 * Created by zhangsong on 2019/9/23.
 */
import { routerRedux } from 'dva/router';
import { formatMessage } from '@/utils/i18n';
import VehicleReducer from '@/dva/application/vehicles';
import * as vehiclesApi from '@/services/application/vehicles';
import Modal from '@/components/Modal';
import { VehicleApplication } from '@/constants/routes';
import { defaultPaginationParam } from '@/constants/styleConfig';
import { mapToSearch } from '@/utils/utils';

const { Namespace, State, Reducers, Effects } = VehicleReducer;

const Model = {
  namespace: Namespace,
  state: State,
  reducers: {
    [Reducers.setState](state, { data = {} } = {}) {
      return {
        ...state,
        ...data,
      };
    },
  },
  effects: {
    *[Effects.saveVehicle]({ data }, { put, call }) {
      yield put({
        type: Effects.setState,
        data: {
          saveLoading: true,
        },
      });
      try {
        yield call(vehiclesApi.saveContractor, data);
        yield Modal.success({
          title: formatMessage('baseInfo.saveSuccess'),
        });
        yield put(routerRedux.push(VehicleApplication.list.url));
      } catch (e) {
        Modal.error({
          title: e.message,
        });
      }
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: false,
        },
      });
    },

    *[Effects.listByParam]({ data = {} }, { put, call }) {
      const {
        page = defaultPaginationParam.page,
        pageSize = defaultPaginationParam.pageSize,
        query = {},
      } = data;
      yield put({
        type: Reducers.setState,
        data: {
          listLoading: true,
        },
      });
      try {
        const response = yield call(vehiclesApi.listByParam, {
          page,
          pageSize,
          ...query,
        });
        yield put({
          type: Reducers.setState,
          data: {
            listLoading: false,
            listData: response.data,
            query,
            page: response.page,
            pageSize: response.pageSize,
            total: response.total,
          },
        });
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(e.message),
        });
      }
    },

    *[Effects.removeById]({ data }, { put, call }) {
      const { page, pageSize, query, id } = data;
      yield put({
        type: Reducers.setState,
        data: {
          listLoading: true,
        },
      });
      try {
        yield call(vehiclesApi.removeById, id);
        yield Modal.success({
          title: formatMessage('baseInfo.removeSuccess'),
        });
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(e.message),
        });
      }
      yield put({
        type: Effects.listByParam,
        data: {
          page,
          pageSize,
          query,
        },
      });
    },

    *[Effects.getVehicleById]({ data }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          detailLoading: true,
          vehicleInfo: {},
        },
      });
      try {
        const response = yield call(vehiclesApi.getById, data);
        yield put({
          type: Effects.setState,
          data: {
            detailLoading: false,
            vehicleInfo: response,
          },
        });
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(e.message),
        });
        yield put(routerRedux.push(VehicleApplication.list.url));
      }
    },

    *[Effects.modifyVehicleById]({ data }, { put, call }) {
      const { body, id, page, pageSize, query = {} } = data;
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      try {
        yield call(vehiclesApi.updateById, {
          id,
          body,
        });
        yield Modal.success({
          title: formatMessage('baseInfo.saveSuccess'),
        });
        yield put(
          routerRedux.push(
            mapToSearch(VehicleApplication.list.url, {
              page,
              pageSize,
              ...query,
            }),
          ),
        );
      } catch (e) {
        yield Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: JSON.stringify(e.message),
        });
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
          },
        });
      }
    },
  },
};

export default Model;
